package com.gxuwz.medical.domain.menu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.dao.MenuDao;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.role.Role;
import com.gxuwz.medical.vo.PageBean;

/**
 * 权限实体类
 * @author 演示
 * 
 */
public class Menu {
	/**
	 * 权限编号
	 */
	private String menuid;
	/**
	 * 权限上一级编号
	 */
	private String menupid="";
	/**
	 * 权限编号
	 */
	private String menuname;
	
	private String url="";
	
	private MenuDao menudao = new MenuDao();
	
	public boolean hasRole(String roleid) throws SQLException{//该菜单是否有关联角色
		return menudao.hasRole(this,roleid);
	}
	
	public void add() throws SQLException {
		createMenuid();
		menudao.add(this);
	}
	public void createMenuid(){
		Connection conn = null;
		Statement stmt=null;
		ResultSet rs =null;
		try{
			conn = new DbUtil().getConn();
			stmt = conn.createStatement();
			String sql ="select max(menuid) from menu where menupid='"+this.menupid+"'";
			rs = stmt.executeQuery(sql);
			String menuidstr="";
			if(rs.next()){
				menuidstr = rs.getString(1);
			}
			if(menuidstr!=null)
				menuidstr = menuidstr.substring(menuidstr.length()-2);
			else
				menuidstr = "00";
			int menuid = Integer.parseInt(menuidstr);
			menuid++;
			this.menuid=this.menupid+String.format("%02d", menuid);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public void delete(String menuid) throws SQLException {
		menudao.delete(menuid);
	}

	public Menu query() throws SQLException {
		return menudao.query(menuid);
	}

	public PageBean queryAll(int page,int size) throws SQLException {
		return menudao.queryAll(page,size);
	}
	public List<Menu> queryAll() throws SQLException {
		return menudao.queryAll();
	}
	

	public List<Menu> findChildren() throws SQLException{
		return menudao.findChildren(this.menuid);
	}
	

	public void update() throws SQLException {
		menudao.update(this);
	}
	
	public Menu(){
		
	}
	public String getMenuid() {
		return menuid;
	}
	public void setMenuid(String menuid) {
		this.menuid = menuid;
	}
	public String getMenupid() {
		return menupid;
	}
	public void setMenupid(String menupid) {
		this.menupid = menupid;
	}
	public String getMenuname() {
		return menuname;
	}
	public void setMenuname(String menuname) {
		this.menuname = menuname;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
}
